/* UniApo:    DBSConfig
 * filename:  main.cpp
 * date:      April 2010
 * author:    Jeffrey Geyssens
 * about:     This program allows the user to setup the database connection.
 *            This entails modifying the db.conf file, located in /UniApo/Saves.
 *            This file contains connection data for two databases.
 *            The actual storage database and a test database.
 *            For each database the configfile stores:
 *            db:        the database name
 *            connecter: functionality presentently excluded.
 *            host:      the database host (local = localhost)
 *            uname:     the username for accessing database
 *            passw:     the password for user account.
 *
 * usage:     Use the following commands to change the following
 *            t to switch between editing storage database and test database
 *            settings. The switch is set to edit the storage database on
 *            program load.
 *            h to modify the host, u to modify the username
 *            p to modify the password
 *            Use s to save and q to quit.
 *
 */

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

int main() {

    //First we get working directory path
    //All source files used in this tool have parent /UniApo/.
    //Path maniplulation operations rely heavily on this property

    char *p = NULL; // y'old C string
    p = getcwd(NULL, 0); // getcwd returns C string of
    string path = p; // put C string is STL C++ String
    string::size_type loc3 = path.find("/UniApo/", 0); //Search for /UniApo/
    stringstream fpath; // introduce string stream for path concatination
    //Create sstream and store results back in path.
    fpath << path.substr(0, loc3 + 8) << "Saves/db.conf";
    path = fpath.str();
    //Declaration DB variables
    string db;
    string connector;
    string host;
    string uname;
    string passw;
    //Declaration of test DB variables
    string tdb;
    string tconnector;
    string thost;
    string tuname;
    string tpassw;
    //Switch for changing modification mode
    bool tswitch = true;
    //Read db.conf contents
    ifstream myfile(path.c_str());
    if (myfile.is_open()) {
        //Read Storage DB data
        getline(myfile, db);
        getline(myfile, connector);
        getline(myfile, host);
        getline(myfile, uname);
        getline(myfile, passw);
        //Read test DB data
        getline(myfile, tdb);
        getline(myfile, tconnector);
        getline(myfile, thost);
        getline(myfile, tuname);
        getline(myfile, tpassw);
        myfile.close();
        //Print data to screen
        cout << "Database Configuration manager" << endl;
        cout << "******************************" << endl;
        cout << "Storage Database Settings" << endl;
        cout << db << endl;
        cout << connector << endl;
        cout << host << endl;
        cout << uname << endl;
        cout << passw << endl;
        cout << endl;
        cout << "Test Database Settings" << endl;
        cout << tdb << endl;
        cout << tconnector << endl;
        cout << thost << endl;
        cout << tuname << endl;
        cout << tpassw << endl << endl << endl;
        cout << "Press q to exit or type m to modify settings" << endl;
        //Check for q to quit or m to modify
        char cont;
        cin >> cont;
        if (cont == 'm') {

            cout << "Use the following commands to change the following";
            cout << endl;
            cout << "t to switch between editing temp_db and db settings";
            cout << endl;
            cout << "h to modify the host, u to modify the username ";
            cout << "or p to modify the password" << endl;
            cout << "Use s to save and q to quit." << endl;

            //Check for commands (t,d,h,u,p,s,q)
            char mod;
            while (mod != 'q') {

                cout << "Enter command (t,d,h,u,p,s,q): ";
                cin >> mod;
                if (mod == 's') {
                    cout << "saving" << endl;
                    ofstream myfile(path.c_str());
                    if (myfile.is_open()) {
                        //Saving storage db data
                        myfile << db.c_str() << "\n";
                        myfile << connector.c_str() << "\n";
                        myfile << host.c_str() << "\n";
                        myfile << uname.c_str() << "\n";
                        myfile << passw.c_str() << "\n";
                        //Saving test db data
                        myfile << tdb.c_str() << "\n";
                        myfile << tconnector.c_str() << "\n";
                        myfile << thost.c_str() << "\n";
                        myfile << tuname.c_str() << "\n";
                        myfile << tpassw.c_str() << "\n";
                        myfile.close();
                    }

                    cout << "file saved" << endl;

                }
                //Switching between edit mode (edit test_db settings or
                //storage db settings (default storage db))
                if (mod == 't') {
                    if (tswitch) {
                        tswitch = false;
                        cout << "mode: test" << endl;
                    } else {
                        tswitch = true;
                        cout << "mode: test off" << endl;
                    }
                }

                if ((mod == 'd') || (mod == 'h') || (mod == 'u')
                        || (mod == 'p')) {
                    //Changing individual variables
                    string change;
                    cout << "New Value: ";
                    cin >> change;
                    if (change != "#") {
                        switch (mod) {

                                //Change Database
                            case 'd':
                            {
                                string dbprefix = "db:";
                                if (tswitch) {
                                    db = dbprefix.append(change);
                                } else {
                                    tdb = dbprefix.append(change);
                                }
                                cout << "DB changed" << endl;


                            }
                                break;
                                //Change host
                            case 'h':

                            {
                                string hostprefix = "host:";
                                if (tswitch) {
                                    host = hostprefix.append(change);
                                } else {
                                    thost = hostprefix.append(change);
                                }
                                cout << "host changed" << endl;

                            }
                                break;
                                //Change username
                            case 'u':

                            {
                                string unameprefix = "uname:";
                                if (tswitch) {
                                    uname = unameprefix.append(change);
                                } else {
                                    tuname = unameprefix.append(change);
                                }
                                cout << "username changed" << endl;

                            }
                                break;

                                //Change password
                            case 'p':
                            {
                                string passwprefix = "passw:";
                                if (tswitch) {
                                    passw = passwprefix.append(change);
                                } else {
                                    tpassw = passwprefix.append(change);
                                }
                                cout << "password changed changed" << endl;

                            }
                                break;
                        }
                    } else {
                        cout << "procedure skipped" << endl;
                    }
                }

            }




        }
    } else {
        cerr << "db.conf missing in /Saves" << endl;
        return 500;
    }
    return 0;
}